package com.woniu.product.app.apiImpl;

import cn.hutool.core.bean.BeanUtil;
import com.woniu.exception.LXLGException;
import com.woniu.product.client.CouponQueryClient;
import com.woniu.product.client.result.CouponResultInfo;
import com.woniu.product.client.result.ReceiveRecordResultInfo;
import com.woniu.product.common.CouponResultCode;
import com.woniu.product.infra.model.Coupon;
import com.woniu.product.infra.model.CouponReceive;
import com.woniu.product.service.CouponReceiveService;
import com.woniu.product.service.CouponService;
import com.woniu.product.service.dto.ReceiveRecordDto;
import com.woniu.product.service.dto.SkuCanUseCouponDto;
import com.woniu.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Slf4j
public class CouponQueryApiImpl implements CouponQueryClient {

    @Resource
    private CouponReceiveService couponReceiveService;

    @Resource
    private CouponService couponService;

    @Override
    public Result receiveRecord(Integer userId, String state) {
        List<ReceiveRecordDto> receiveRecordDtos = couponReceiveService.receiveRecord(userId, state);
        List<ReceiveRecordResultInfo> receiveRecordResultInfos = BeanUtil.copyToList(receiveRecordDtos, ReceiveRecordResultInfo.class);

        return Result.getInstance(List.class).setData(receiveRecordResultInfos);
    }

    /**
     * 根据优惠券领取Id查询信息
     *
     * @param receiveRecordId
     * @return
     */
    @Override
    public Result<ReceiveRecordResultInfo> serachCuponByCouponReceiveId(Integer receiveRecordId) {
        log.info("根据优惠券领取Id查询信息:{}", receiveRecordId);
        CouponReceive couponReceive = couponReceiveService.getById(receiveRecordId);
        ReceiveRecordResultInfo receiveRecordResultInfo = BeanUtil.copyProperties(couponReceive, ReceiveRecordResultInfo.class);
        return Result.getInstance(ReceiveRecordResultInfo.class).setData(receiveRecordResultInfo);
    }

    /**
     * 查询skuId可使用的券
     *
     * @param skuId
     * @return
     */
    @Override
    public Result<List<CouponResultInfo>> searchCouponBySkuId(Integer skuId) {
        log.info("查询skuId可使用的券:{}", skuId);
        List<SkuCanUseCouponDto> skuCanUseCouponDtos = couponService.searchCouponBySkuId(skuId);
        List<CouponResultInfo> couponResultInfos = BeanUtil.copyToList(skuCanUseCouponDtos, CouponResultInfo.class);
        return Result.getInstanceList(CouponResultInfo.class).setData(couponResultInfos);
    }

    /**
     * 根据couponId查询优惠券信息
     *
     * @param couponId
     * @return
     */
    @Override
    public Result<CouponResultInfo> searchCouponByCouponId(Integer couponId) {
        Coupon coupon = couponService.getById(couponId);
        if (coupon == null) {
            throw new LXLGException(CouponResultCode.COUPONLOGS_NOT_FOUND);
        }
        CouponResultInfo couponResultInfo = BeanUtil.copyProperties(coupon, CouponResultInfo.class);
        return Result.getInstance(CouponResultInfo.class).setData(couponResultInfo);
    }


}
